package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.FuelTypeQueryConditionVO;
import com.zhiche.lisa.bms.pojo.vo.driver.FuelTypeVO;
import com.zhiche.lisa.bms.service.driver.IFuelTypeService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 燃油类型表 前端控制器
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/fuel")
@Slf4j
@Api(description = "燃油类型表", tags = {"燃油类型表"})
public class FuelTypeController {

    @Autowired
    private IFuelTypeService fuelTypeService;

    /**
     * 分页显示燃油类型
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页显示燃油类型表", notes = "分页显示燃油类型表",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelTypeVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<FuelTypeVO>> listByPage(@ApiParam(name = "FuelTypeVO", value = "燃油类型VO", required = true) @RequestBody Page<FuelTypeVO> page) {
        log.info("FuelTypeController.listByPage params : {}", page);
        RestfulResponse<Page<FuelTypeVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<FuelTypeVO> pageVo = fuelTypeService.listByPage(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            log.error("FuelTypeController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    /**
     * 添加燃油类型
     *
     * @param fuelTypeVO
     * @param br
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加燃油类型", notes = "添加燃油类型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "FuelTypeVO", value = "线路VO", required = true) @RequestBody @Valid FuelTypeVO fuelTypeVO, BindingResult br) {
        log.info("FuelTypeController.add params : {},{}", fuelTypeVO, br);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Integer id = fuelTypeService.add(fuelTypeVO, br);
            result.setData(id);
        } catch (BaseException e) {
            log.error("FuelTypeController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 更新燃油类型
     *
     * @param fuelTypeVO
     * @param br
     * @return
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新燃油类型", notes = "更新燃油类型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "FuelTypeVO", value = "线路VO", required = true) @RequestBody @Valid FuelTypeVO fuelTypeVO, BindingResult br) {
        log.info("FuelTypeController.update param : {}", fuelTypeVO);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Boolean blrRtn = fuelTypeService.update(fuelTypeVO, br);
            result.setData(blrRtn);
        } catch (BaseException e) {
            log.error("FuelTypeController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/del")
    @ApiOperation(value = "删除燃油类型", notes = "删除燃油类型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "车型ID", required = true)})
    public RestfulResponse<Boolean> del(Integer id) {
        log.info("FuelTypeController.del param : {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Boolean blrRtn = fuelTypeService.del(id);
            result.setData(blrRtn);
        } catch (BaseException e) {
            log.error("FuelTypeController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据ID得到燃油信息", notes = "根据ID得到燃油信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "id", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelTypeVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<FuelTypeVO> getById(Integer id) {
        log.info("DFuelTypeController.getById param: {}", id);
        RestfulResponse<FuelTypeVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            FuelTypeVO fuelTypeVO = fuelTypeService.getById(id);
            result.setData(fuelTypeVO);
        } catch (BaseException e) {
            log.error("FuelTypeController.getById ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.getById ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @GetMapping(value = "/list")
    @ApiOperation(value = "列表显示燃油类型", notes = "列表显示燃油类型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "fuelCode", value = "燃油编码"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fuelName", value = "燃油名称")})
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FuelTypeVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<FuelTypeVO>> list(FuelTypeQueryConditionVO conditions) {
        log.info("DFuelTypeController.list param: {}", conditions);
        RestfulResponse<List<FuelTypeVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<FuelTypeVO> fuelTypeList = fuelTypeService.selectListByConditions(conditions);
            result.setData(fuelTypeList);
        } catch (BaseException e) {
            log.error("FuelTypeController.list ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("FuelTypeController.list ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

}

